package com.teacher.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.teacher.common.CommonResult;
import com.teacher.common.JwtTokenUtil;
import com.teacher.pojo.dto.notice.NoticeAddDto;
import com.teacher.pojo.dto.notice.NoticeGetDto;
import com.teacher.pojo.entity.Notice;
import com.teacher.mapper.NoticeMapper;
import com.teacher.pojo.enums.NoticeType;
import com.teacher.pojo.vo.notice.NoticeGetVo;
import com.teacher.service.INoticeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 公告 服务实现类
 * </p>
 *
 * @author zfm
 * @since 2021-09-01
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements INoticeService {


    @Autowired
    private HttpServletRequest httpServletRequest;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    /**
     * 添加公告
     *
     * @param dto
     * @return
     */
    @Override
    public CommonResult<String> add(NoticeAddDto dto) {
        Notice notice = new Notice();
        BeanUtils.copyProperties(dto, notice);
        notice.setTeacherKey(jwtTokenUtil.getUsernameByRequest(httpServletRequest));
        if (this.save(notice)) {
            return CommonResult.success("添加成功");
        }
        return CommonResult.failed("添加失败");
    }

    /**
     * 查询公告
     *
     * @param dto
     * @return
     */
    @Override
    public CommonResult<IPage<NoticeGetVo>> getPage(NoticeGetDto dto) {
        Page<NoticeGetVo> page = new Page<>(dto.getCurrentPage(), dto.getPageSize());
        IPage<NoticeGetVo> iPage = baseMapper.getPage(page, dto);
        return CommonResult.success(iPage);
    }

    /**
     * 普通用户查询公告
     *
     * @param dto
     * @return
     */
    @Override
    public CommonResult<IPage<NoticeGetVo>> getPageByCustomer(NoticeGetDto dto) {
        Page<NoticeGetVo> page = new Page<>(dto.getCurrentPage(), dto.getPageSize());
        dto.setStatus(NoticeType.UP.getCode());
        IPage<NoticeGetVo> iPage = baseMapper.getPage(page, dto);
        return CommonResult.success(iPage);
    }
}
